#!/usr/bin/env -S deno run --allow-net

import { GogsClient } from "../index.ts";

async function main() {
  // 从环境变量或命令行参数获取配置
  const url = Deno.env.get("GOGS_URL") || "http://localhost:3000";
  const token = Deno.env.get("GOGS_TOKEN") || "your-access-token";
  
  console.log(`连接到 Gogs 服务器: ${url}`);
  
  // 创建客户端
  const client = new GogsClient({
    url,
    token,
  });
  
  try {
    // 获取客户端版本
    console.log(`客户端版本: ${client.getVersion()}`);
    
    // 获取当前用户信息
    console.log("获取当前用户信息...");
    const user = await client.users.getSelf();
    console.log(`当前用户: ${user.username} (${user.email})`);
    
    // 列出用户仓库
    console.log("\n列出用户仓库:");
    const repos = await client.repos.listMyRepos();
    
    if (repos.length === 0) {
      console.log("没有找到仓库");
    } else {
      repos.forEach(repo => {
        console.log(`- ${repo.full_name} (${repo.private ? "私有" : "公开"})`);
      });
      
      // 获取第一个仓库的问题
      const firstRepo = repos[0];
      console.log(`\n获取仓库 ${firstRepo.full_name} 的问题:`);
      
      const issues = await client.issues.listRepoIssues(
        firstRepo.owner.username,
        firstRepo.name
      );
      
      if (issues.length === 0) {
        console.log("没有找到问题");
      } else {
        issues.forEach(issue => {
          console.log(`- #${issue.number}: ${issue.title} (${issue.state})`);
        });
      }
    }
  } catch (error: unknown) {
    console.error("发生错误:", error instanceof Error ? error.message : String(error));
  }
}

// 运行主函数
if (import.meta.main) {
  main().catch(console.error);
} 